package view;

import logic.*;

import java.util.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

/**
 *
 * Provide a graphical view of a pie.
 * the slices are standing for individual species wich are at the field.
 * @author Vossen en Konijnen groep 2
 */
public class PieView extends AbstractView {
	// counts for the various species
	private int hunterCount;
	private int duckCount;
	private int foxCount;
	private int rabbitCount;
	private int grassCount;
	private int totalCount;
	private FieldStats stats;
	
	/**
	 * constructor for a pieView
	 * @param simulator
	 */
	public PieView(Simulator simulator) {
		super(simulator);
		setSize(200, 200);
	}

	/**
	 * makes that the pieview is being drawn
	 */
	public void paintComponent(Graphics g) {
		// sets the background color
		g.setColor(new Color(238,238,238));
		Dimension d=getSize();
		g.fillRect(0,0,d.width,d.height);
		// get the fieldstats of the current field
		stats = getModel().getStats();
		HashMap hashmap = stats.getHashMap();
		try{
			// get the counter for the individual species
			foxCount = ((Counter) hashmap.get(Fox.class)).getCount();
			duckCount = ((Counter) hashmap.get(Duck.class)).getCount();
			hunterCount = ((Counter) hashmap.get(Hunter.class)).getCount();
			rabbitCount = ((Counter) hashmap.get(Rabbit.class)).getCount();
			grassCount = ((Counter) hashmap.get(Grass.class)).getCount();
			totalCount = foxCount+duckCount+hunterCount+rabbitCount+grassCount;
		}	
		catch(Exception e)
		{
			
		}
		// set the color for the species and draw the slices of the pie
		g.setColor(Color.BLUE);
		g.fillArc(10, 10, 200, 200, 0 , setDegree(foxCount));
		g.setColor(Color.BLACK);
		g.fillArc(10,10,200,200, setDegree(foxCount) , setDegree(hunterCount));
		g.setColor(Color.RED);
		g.fillArc(10,10,200,200, setDegree(foxCount+hunterCount) , setDegree(duckCount));
		g.setColor(Color.ORANGE);
		g.fillArc(10,10,200,200, setDegree(foxCount+hunterCount+duckCount) , setDegree(rabbitCount));
		g.setColor(Color.GREEN);
		g.fillArc(10,10,200,200, setDegree(foxCount+hunterCount+duckCount+rabbitCount), setDegree(grassCount));//360-setDegree(foxCount+hunterCount+duckCount+rabbitCount));
	}	
	
	/**
	 * sets the angle of the slice 
	 * @param count
	 * @return the angle
	 */
	private int setDegree(int count)
	{
		return (int)Math.round(360.0*(count)/totalCount);
	} 
}